package com.topvision.camerasdk.video;

import android.content.Context;
import android.media.CamcorderProfile;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.topvision.camerasdk.R;
import com.topvision.commomsdk.TouchInterface;
import com.topvision.commomsdk.uitl.PreferencesUtils;
import com.topvision.commomsdk.view.TvViewPager;
import com.topvision.voicesdk.BaseVoiceActivity;
import com.topvision.voicesdk.CommonVoice;
import com.topvision.voicesdk.VoiceInterface;

import java.util.ArrayList;

public class VideoSettingActivity extends BaseVoiceActivity implements TouchInterface,VoiceInterface {
    /**
     * 设置分辨率设置
     *
     * @author joker
     *
     */
    private TvViewPager viewPager;
    private View view1, view2, view3;
    private ArrayList<View> viewList;
    public final static String SHAREPREFERENCE_RESOLUTION = "resolution";
    Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        mContext = this;
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        setTouchInterfaceListener(this);
        setVoiceInterfaceListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void initView() {
        viewPager = (TvViewPager) findViewById(R.id.video_set_viewpager);
        viewPager.setNoScroll(true);
        LayoutInflater inflater = getLayoutInflater();
        view1 = inflater.inflate(R.layout.video_setting_itme, null);
        view2 = inflater.inflate(R.layout.video_setting_itme2, null);
        view3 = inflater.inflate(R.layout.video_setting_itme3, null);
        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewPager.setAdapter(pagerAdapter);
        showCurrentResolution();
    }

    /**
     *显示当前用户设置的分辨率
     *create at 2017/2/7 10:10
     */
    private void showCurrentResolution() {
        int resolution =  PreferencesUtils.getInt(mContext,SHAREPREFERENCE_RESOLUTION, CamcorderProfile.QUALITY_480P);
        switch (resolution){
            case CamcorderProfile.QUALITY_480P:
                viewPager.setCurrentItem(0);
                break;
            case CamcorderProfile.QUALITY_720P:
                viewPager.setCurrentItem(1);
                break;
            case CamcorderProfile.QUALITY_1080P:
                viewPager.setCurrentItem(2);
                break;
        }
    }


    PagerAdapter pagerAdapter = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }
    };

    @Override
    public void doForwardTouch() {
        int currentItem = viewPager.getCurrentItem();
        viewPager.setCurrentItem(currentItem + 1);
    }

    @Override
    public void doBackwardTouch() {
        int currentItem = viewPager.getCurrentItem();
        viewPager.setCurrentItem(currentItem - 1);
    }

    @Override
    public void doClickTouch() {
        int currentItem = viewPager.getCurrentItem();
        choose(currentItem);
    }

    @Override
    public void doDoubleTouch() {

    }

    public void choose(int i) {
        switch (i) {
            case 0:
                PreferencesUtils.putInt(VideoSettingActivity.this, SHAREPREFERENCE_RESOLUTION,
                        CamcorderProfile.QUALITY_480P);
                break;
            case 1:
                PreferencesUtils.putInt(VideoSettingActivity.this, SHAREPREFERENCE_RESOLUTION,
                        CamcorderProfile.QUALITY_720P);
                break;
            case 2:
                PreferencesUtils.putInt(VideoSettingActivity.this, SHAREPREFERENCE_RESOLUTION,
                        CamcorderProfile.QUALITY_1080P);
                break;
        }
        finish();
    }

    @Override
    public void dealWithVoice(String data) {
        if (data.equals(CommonVoice.VOICE_ZH_DOWN) || data.equalsIgnoreCase(CommonVoice.VOICE_US_DOWN)) {
            if (viewPager != null) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            }
        } else if (data.equals(CommonVoice.VOICE_ZH_UP) || data.equalsIgnoreCase(CommonVoice.VOICE_US_UP)) {
            if (viewPager != null) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
            }
        } else if (data.equals(CommonVoice.VOICE_ZH_OK) || data.equalsIgnoreCase(CommonVoice.VOICE_US_OK) || data.equalsIgnoreCase(CommonVoice.VOICE_US_CONFIRM))
            if (viewPager != null) {
                int currentIndex = viewPager.getCurrentItem();
                choose(currentIndex);

            }
    }


}
